<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="项目比赛场地" prop="projectSite">
        <el-input
          v-model="queryParams.projectSite"
          placeholder="请输入项目比赛场地名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:site:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:site:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:site:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:site:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="siteList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="65" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="赛区" align="center" prop="siteName" />
      <el-table-column label="比赛场地" align="center" prop="projectSite" />
<!--      <el-table-column label="比赛项目ID" align="center" prop="parentId" />-->
<!--      <el-table-column label="项目名称" align="center" prop="projectName" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="primary"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:site:edit']"
          >修改</el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:site:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改新增比赛场地对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="赛区" prop="siteId">
          <el-select v-model="form.siteId" placeholder="请选择参与项目" clearable>
            <el-option
              v-for="item in option"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="比赛项目" prop="parentId">
          <el-select v-model="form.parentId"  placeholder="请选择参与项目" clearable>
            <el-option
              v-for="item in projectList"
              :key="item.parentId"
              :label="item.projectName"
              :value="item.parentId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="项目比赛场地" prop="projectSite">
          <el-input v-model="form.projectSite" placeholder="请输入项目比赛场地" />
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSite, getSite, delSite, addSite, updateSite, queryList} from "@/api/system/site";

export default {
  name: "Site",
  data() {
    return {
      option:[
        {value:1,label:'北京赛区'},
        {value:2,label:'北京延庆赛区'},
        {value:3,label:'张家口赛区'}
      ],
      projectList:[],
      projectTotalList:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 新增比赛场地表格数据
      siteList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectSite: null,
        parentId: null,
      },
      // 表单参数
      form: {
        projectName:null
      },
      // 表单校验
      rules: {
        projectSite:[ {required:true, message:'比赛场地不能为空',trigger: "change"}],
        siteId:[{required:true, message:'项目名称不能为空',trigger: "change"}]
      }
    };
  },
  created() {
    this.getList();
    this.queryProjectList();
  },
  methods: {
    queryProjectList(){
      queryList().then (response => {
          this.projectTotalList = response.rows;
          for( let i=0;  i<this.projectTotalList.length; i++){
              const arr ={parentId:this.projectTotalList[i].id,projectName:this.projectTotalList[i].projectName}
              this.projectList =this.projectList.concat(arr)
          }
          this.total = response.total;
          this.loading = false;
        });
    },
    /** 查询新增比赛场地列表 */
    getList() {
      this.loading = true;
      listSite(this.queryParams).then(response => {
        this.siteList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        projectSite: null,
        parentId: null,
        extField: null,
        extField1: null,
        extField2: null,
        isDeleted: null,
        createUserid: null,
        createUsername: null,
        createTime: null,
        serverIp: null,
        updateUserid: null,
        updateUsername: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加新增比赛场地";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSite(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改新增比赛场地";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          const self = this
          delete self.form.createTime
          delete self.form.createUserid
          delete self.form.createUsername
          delete self.form.extField
          delete self.form.extField1
          delete self.form.extField2
          delete self.form.isDeleted
          delete self.form.remark
          delete self.form.serverIp
          delete self.form.updateTime
          delete self.form.updateUserid
          delete self.form.updateUsername
          for (let i =0; i<self.projectList.length;i++){
            debugger
            if(self.form.parentId == self.projectList[i].parentId){
              self.form.projectName = self.projectList[i].projectName
            }
          }
          debugger
          if(self.form.siteId == 1 ){
            self.form.siteName = '北京赛区'
          }else if(self.form.siteId == 2){
            self.form.siteName = '北京延庆赛区'
          }else if (self.form.siteId == 2){
            self.form.siteName = '张家口赛区'
          }
          if ( self.form.id != null) {
            updateSite(self.form).then(response => {
              self.$modal.msgSuccess("修改成功");
              self.open = false;
              self.getList();
            });
          } else {
            debugger
            addSite(self.form).then(response => {
              self.$modal.msgSuccess("新增成功");
              self.open = false;
              self.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除新增比赛场地编号为"' + ids + '"的数据项？').then(function() {
        return delSite(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/site/export', {
        ...this.queryParams
      }, `site_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
